﻿using Hmc.IM.Common.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Hmc.IM.Client.Core
{
    public class CommandBusiness
    {
        /// <summary>
        /// 命令处理
        /// </summary>
        /// <param name="commandText"></param>
        /// <returns></returns>
        public static CommandResultEntity Execute(string commandText)
        {
            if (string.IsNullOrEmpty(commandText)) throw new ArgumentNullException("未知的命令，命令不能为空");
            string[] commands = commandText.Split('|');
            if (commands.Length != 2) throw new ArgumentException("未知的命令，合格的命令格式为int|string");
            int commandType = -1;
            if (int.TryParse(commands[0], out commandType) == false) throw new ArgumentException("未知的命令，commandtype is not int");

            CommandResultEntity resultEntity = new CommandResultEntity();

            switch (commandType)
            {
                case (int)EnumDirectiveType.Login:
                    resultEntity = ExecuteLoginCommand(commands[1]);
                    break;
                case (int)EnumDirectiveType.HandClasp:
                    resultEntity = ExecuteHandClaspCommand(commands[1]);
                    break;
                case (int)EnumDirectiveType.Hint:
                    resultEntity = ExecuteHintCommand(commands[1]);
                    break;
            }
            return resultEntity;
        }

        /// <summary>
        /// 提醒
        /// </summary>
        /// <param name="commandText"></param>
        /// <returns></returns>
        private static CommandResultEntity ExecuteHintCommand(string commandText)
        {
            CommandResultEntity resultEntity = new CommandResultEntity();
            resultEntity.CommandType = EnumDirectiveType.Hint;
            string[] resultStr = commandText.Split(',');

            if (Boolean.Parse(resultStr[0]) == false) resultEntity.Result = false;
            else if (Boolean.Parse(resultStr[0])) resultEntity.Result = true;

            resultEntity.Message = resultStr[1];
            return resultEntity;
        }

        /// <summary>
        /// 握手
        /// </summary>
        /// <param name="commandText"></param>
        /// <returns></returns>
        private static CommandResultEntity ExecuteHandClaspCommand(string commandText)
        {
            CommandResultEntity resultEntity = new CommandResultEntity();
            resultEntity.CommandType = EnumDirectiveType.HandClasp;
            string[] resultStr = commandText.Split(',');

            if (Boolean.Parse(resultStr[0]) == false) resultEntity.Result = false;
            else if (Boolean.Parse(resultStr[0])) resultEntity.Result = true;

            resultEntity.Message = resultStr[1];
            return resultEntity;
        }

        /// <summary>
        /// 登录处理
        /// </summary>
        /// <param name="commandText"></param>
        /// <returns></returns>
        private static CommandResultEntity ExecuteLoginCommand(string commandText)
        {
            CommandResultEntity resultEntity = new CommandResultEntity();
            resultEntity.CommandType = EnumDirectiveType.Login;

            string[] resultStr = commandText.Split(',');

            if (Boolean.Parse(resultStr[0]) == false) resultEntity.Result = false;
            else if (Boolean.Parse(resultStr[0])) resultEntity.Result = true;

            resultEntity.Message = resultStr[1];
            return resultEntity;
        }
    }

    public class CommandResultEntity
    {
        public bool Result { get; set; }
        public string Message { get; set; }
        public EnumDirectiveType CommandType { get; set; }

    }
}
